function getRankings() {
    return new Promise(function (resolve, reject) {
        const url = '/api/rankings';
        fetch(`${url}`, {
            method: 'get',
            headers: new Headers({
                'Authorization': `Bearer ${localStorage.getItem('token')}`
            })
        })
            .then((res) => {
                return res.json();
            })
            .then((res) => {
                resolve(res);
            }).catch(function (err) {
                reject(err);
            });
    });
}

async function showRankings() {
    let rankingsTable = '<tr><th>排名</th><th>姓名</th><th>成绩</th><th>学号</th></tr>';
    let rankings = await getRankings();
    rankings = JSON.parse(JSON.stringify(rankings.data));
    
    for (let i = 0; i < rankings.length; i++) {
        rankingsTable += `<tr>
                            <td>${i + 1}</td>
                            <td>${rankings[i].name}</td>
                            <td>${rankings[i].results}</td>
                            <td>${rankings[i].schoolNumber}</td>
                        </tr>`;
    }
    document.querySelector('.main__rankings').innerHTML = rankingsTable;
}

showRankings();